/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */
#include "GenericFont.h"
#include "AbstractFont.h"
#include <xml/Stream.h>
#include "Proportional.h"
#include "TrueType.h"
#include <Gluck.h>
#include <resource/Manager.h>
#include <lib3d/node/text/SymbolAtlas.h>

namespace liba {
namespace font {

namespace {

class ObjectThatAlwaysLoads
	: public xml::ObjectUnicode
{
public:
	virtual bool on_attribute( xml::Provider * prov, const std::wstring & name, const std::wstring & value )
	{
		return true;
	}

	virtual bool on_node( xml::Provider * prov, const std::wstring & name )
	{
		return true;
	}
};

}

GenericFontRep::GenericFontRep(const filesystem::String & name)
{
	ObjectThatAlwaysLoads o;

	std::auto_ptr<resource::Resource> res(resource::Manager::create_resource( name ));
	if (xml::StreamResource(res.get()).load( &xml::Root(&o, "Font") ))
		font.reset(new Proportional(res.get(), name));
	else
	{
		res->reset();
		if (xml::StreamResource(res.get()).load( &xml::Root(&o, "TrueTypeFont") ))
		{
			res->reset();
			font.reset(new TrueType(res.get(), name));
		}
		else
		{
			res->reset();
			if (xml::StreamResource(res.get()).load( &xml::Root(&o, "SymbolAtlas") ))
			{
				res->reset();
				font.reset(new lib3d::node::text::SymbolAtlas(name));
			}
			else
				THROW(GluckBadResource( __FUNCTION__, "load() failed", name));
		}
	}
}

GenericFontRep::~GenericFontRep()
{
}

} // namespace font
} // namespace liba